const { getMsg } = require("./code.js");

// 响应结构体
class Response {
  constructor(code, message, data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }
}

// 返回 JSON 数据
function returnJson(res, httpCode, code, msg, data) {
  res.status(httpCode).json(new Response(code, msg, data));
}

// 语法糖函数封装

// 自定义 httpCode, code, data
function send(res, httpCode, code, data) {
  returnJson(res, httpCode, code, getMsg(code), data);
}

// 自动根据 code 获取 message, 且 data == null
function sendCode(res, code) {
  send(res, 200, code, null);
}

// 自动根据 code 获取 message, 且 data != null
function sendData(res, code, data) {
  send(res, 200, code, data);
}

function successData(res, data) {
  send(res, 200, 0, data); // 0直接对应code中的ok
}

function success(res) {
  send(res, 200, 0, null);
}

//导出多个内容
module.exports = {
  sendCode,
  sendData,
  successData,
  success,
  returnJson,
};
